package jrunner;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;


public class ExportPersonDataToReadableFile implements ExportPersonData {

	public void exportPersonData(Person p) {

		Person person = p;
		
		//put all data from the person object into one String
		StringBuilder builder = new StringBuilder();
		
		builder.append("<name>"); builder.append(person.getName()); builder.append("</name> \n");
		builder.append("<age>"); builder.append(person.getAge()); builder.append("</age> \n");
		builder.append("<height>"); builder.append(person.getHeight()); builder.append("</height> \n");
		builder.append("<weight>"); builder.append(person.getWeight()); builder.append("</weight> \n");
		builder.append("<female>"); builder.append(person.getFemale()); builder.append("</female> \n");
		builder.append("<male>"); builder.append(person.getMale()); builder.append("</male> \n");
		
		String fileInput = builder.toString();
		
		//Export the data into a humanreadable file
		
		//Create a directory
		File dir = new File("personData");
		
		if(!dir.exists()){
			dir.mkdir();
		}
		
//		Create the file
		File file = new File(dir.getName()+"/"+person.getName()+".person");
		
		if (!file.exists()){
			try {file.createNewFile();}catch (IOException e) {e.printStackTrace();}
		}
		
//		Write the file
		try {
			PrintWriter writer = new PrintWriter(file);
			writer.write(fileInput);
			writer.close();
		}catch (FileNotFoundException e) {e.printStackTrace();}

	}

}
